package com.moodbox.server.mood.controller;

import com.moodbox.server.common.response.Result;
import com.moodbox.server.mood.dto.MoodRecordDTO;
import com.moodbox.server.mood.entity.MoodRecord;
import com.moodbox.server.mood.service.MoodRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 心情记录控制器
 */
@RestController
@RequestMapping("/api/mood-records")
@RequiredArgsConstructor
@Api(tags = "心情记录管理")
public class MoodRecordController {

    @Resource
    private final MoodRecordService moodRecordService;

    @PostMapping
    @ApiOperation("创建心情记录")
    public Result<MoodRecord> createMoodRecord(
        HttpServletRequest request,
        @Valid @RequestBody MoodRecordDTO moodRecordDTO
    ) {
        String userId = (String) request.getAttribute("userId");
        return Result.success(moodRecordService.createMoodRecord(userId, moodRecordDTO));
    }

    @PutMapping("/{recordId}")
    @ApiOperation("更新心情记录")
    public Result<MoodRecord> updateMoodRecord(
        HttpServletRequest request,
        @ApiParam("记录ID") @PathVariable String recordId,
        @Valid @RequestBody MoodRecordDTO moodRecordDTO
    ) {
        String userId = (String) request.getAttribute("userId");
        moodRecordDTO.setId(recordId);
        return Result.success(moodRecordService.updateMoodRecord(userId, moodRecordDTO));
    }

    @GetMapping("/date")
    @ApiOperation("获取特定日期的心情记录")
    public Result<MoodRecord> getMoodRecordByDate(
        HttpServletRequest request,
        @ApiParam("记录日期") @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime recordDate
    ) {
        String userId = (String) request.getAttribute("userId");
        return Result.success(moodRecordService.getMoodRecordByDate(userId, recordDate));
    }

    @GetMapping
    @ApiOperation("获取心情记录列表")
    public Result<List<MoodRecord>> listMoodRecords(
        HttpServletRequest request,
        @ApiParam("开始日期") @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
        @ApiParam("结束日期") @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate
    ) {
        String userId = (String) request.getAttribute("userId");
        return Result.success(moodRecordService.listMoodRecords(userId, startDate, endDate));
    }

    @DeleteMapping("/{recordId}")
    @ApiOperation("删除心情记录")
    public Result<Boolean> deleteMoodRecord(
        HttpServletRequest request,
        @ApiParam("记录ID") @PathVariable String recordId
    ) {
        String userId = (String) request.getAttribute("userId");
        return Result.success(moodRecordService.deleteMoodRecord(userId, recordId));
    }
} 